#! /usr/local/bin/python

# read file
import sys

openfile = open( sys.argv[1], 'r' )
content = openfile.readlines()

quiz = []
quizQuestion = []

for questionItem in content:
	# take a line
	questionLine = questionItem.strip()

	if questionLine == '':
		continue

	# if the line is a question designated by the hash
	# take the other lines.  at this point, questionLine
	# is the formatted question
	if questionLine.find( '#' ) <> -1:
		quizQuestion = []
		quizQuestion.append( questionLine.strip( '#' ) )
	else:
		quizQuestion.append( questionLine )
	
	if questionLine.find( '#' ) <> -1:
		quiz.append( quizQuestion )

openfile.close()

###########################
# from here, we have the quiz object. each item in the list
# has 2+ items with the first items as the quiz question
# and the remaining items as answers	

theQuiz = []

for qObject in quiz:
	quizItem = []
	counter = 1
	for qItem in qObject:
		if counter == 1:
			quizItem.append( qItem )
		if counter > 1:
			# choice object: 1 - answer, 2 - correct
			oChoice = []
			if qItem.find( '^' ) <> -1:
				oChoice.append( qItem.strip( '^' ) );
				oChoice.append( 'true' );
			else:
				oChoice.append( qItem );
				oChoice.append( 'false' );
			quizItem.append( oChoice )
		counter = counter + 1

	theQuiz.append( quizItem )
#print theQuiz
##### print the HTML

openwritefile = open( sys.argv[2], 'w' )
openwritefile.write( "<form action='javascript:processQuiz()' name='quiz'>" )
openwritefile.write( "<ol>" )
counter = 0

for qItem in theQuiz:
	openwritefile.write( "<li><p class='question-title' id='question-title" + str( counter ) + "'>" + qItem[ 0 ] + "</p>\n" )
	qName = "q" + str( counter )
	
	answerCounter = 0
	for qAnswer in qItem:
		if answerCounter == 0:
			answerCounter = answerCounter + 1
			continue
		else:
			openwritefile.write( "<input name='" + qName + "' type='radio'" )
			if qAnswer[ 1 ] == "true":
				openwritefile.write( "value='1'/>\n" )
			else:
				openwritefile.write( "value='0'/>\n" )
			openwritefile.write( qAnswer[ 0 ] + "<br/>\n" )
			answerCounter = answerCounter + 1
	openwritefile.write( "<br/>\n" )
	counter = counter + 1

openwritefile.write( "</ul>" )
openwritefile.write( "<br/><input type='submit' name='submit' value='Submit Your Answers'/>\n" )
openwritefile.write( "<input type='reset' name='reset' value='Clear Answers'/>\n" )
openwritefile.write( "</form>" )



